package org.skipper.common;


import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:分页封装类
 *
 * @author shoujian
 * Date: 2017-10-18
 * Time: 下午3:53
 */
public class PageResult<T> extends DTO {

    private static final long serialVersionUID = -6863723897317167135L;

    /**
     * 总条数
     */
    private long count;
    /**
     * 当前页码
     */
    private long pageNum;
    /**
     * 每页数量
     */
    private long pageCount;

    /**
     * 总页数
     */
    private long pages;

    private List<T> items = new ArrayList();

    public PageResult() {
        this(0, 0, 0, null);
    }

    public PageResult(long count, long pageNum, long pageCount, List<T> items) {
        this.count = count;
        this.pageNum = pageNum;
        this.pageCount = pageCount;
        this.items = items;
        setPages();
    }

    public PageResult(long count, long pageNum, long pageCount, List<T> items, long pages) {
        this.count = count;
        this.pageNum = pageNum;
        this.pageCount = pageCount;
        this.items = items;
        this.pages = pages;
    }

    @Override
    public String toString() {
        return "PageResult{" +
                "count=" + count +
                ", pageNum=" + pageNum +
                ", pageCount=" + pageCount +
                ", items=" + items +
                ", pages=" + pages +
                '}';
    }

    public long getCount() {
        return this.count;
    }

    public void setCount(long count) {
        this.count = count;
        setPages();
    }

    public List<T> getItems() {
        return this.items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }

    public long getPageNum() {
        return pageNum;
    }

    public void setPageNum(long pageNum) {
        this.pageNum = pageNum;
    }

    public long getPageCount() {
        return pageCount;
    }

    public void setPageCount(long pageCount) {
        this.pageCount = pageCount;
        setPages();
    }

    public long getPages() {
        return pages;
    }

    private void setPages() {
        long count = this.count < 0 ? 0 : this.count;
        long pageCount = this.pageCount < 0 ? 0 : this.pageCount;
        if (count == 0 || pageCount == 0) {
            this.pages = 0;
        } else {
            this.pages = count / pageCount;
            if (count % pageCount != 0) {
                this.pages += 1;
            }
        }
    }

}
